<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<html>
<head>
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/luna/tab.css" />
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/style.css"/>
<script type="text/javascript" src="${ctx}/js/xtable.js"></script>
<script type="text/javascript" src="${ctx}/js/tabpane.js"></script>
<script type="text/javascript" src="${ctx}/js/validator.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/SecurityService.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateService.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateColumnService.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateColumnEnumService.js"></script>
<script type="text/javascript" src="${ctx}/js/loading.js"></script>
<title>角色管理</title>
</head>
<body onload="init()">
<table>
    <tr>
        <td align="center" valign="top" style="width:30%" nowrap>
            <form id="templateForm" name="templateForm" action="">
            模版名称：
            <input type="text" id="templateName" name="templateName"/>
            <img src="${ctx}/images/common/button_insert.jpg" title="添加模板" style="cursor:pointer" onclick="insertTemplate()"/>
            <img src="${ctx}/images/common/button_update.jpg" title="修改模板" style="cursor:pointer" onclick="updateTemplate()"/>
            <img src="${ctx}/images/common/button_delete.jpg" title="删除模板" style="cursor:pointer" onclick="deleteTemplate()"/>
            </form>
            <SCRIPT LANGUAGE="JavaScript" type="">
            var templateXThead = new Array(
            new XThead("选择", "radio"),
            new XThead("模版名称")
            );

            var templateXTable = new XTable("template", templateXThead, true);

            templateXTable.XTbodyArray = function (model) {
                var array = new Array(
                new XTbody("template", model.templateId, "radio", "onclick='setTemplate(this)'", model),
                new XTbody("template", model.templateName)
                );
                return array;
            }

            templateXTable.getXList = function (page){
                TemplateService.selectTemplate(null, function(list){templateXTable.addTable(list);});
            }

            function selectTemplate(){
                templateXTable.goPage(1);
            }
            </SCRIPT>
        </td>
        <td>&nbsp;</td>
        <td valign="top">
            <div id="tabPane1">
                <script type="text/javascript">
                var tabPane1 = new WebFXTabPane(document.getElementById("tabPane1"));
                </script>
                <div class="tab-page" id="tabPage1_2" width="100%">
                    <h2 class="tab">
                    模板配置
                    </h2>
                    <script type="text/javascript">
                    tabPane1.addTabPage(document.getElementById("tabPage1_2"));
                    </script>
                    <form id="templateColumnForm" name="templateColumnForm" action="">
                    <table>
                        <tr>
                            <td>
                            字段名称：
                            </td>
                            <td>
                                <input type="text" id="columnName" name="templateName" elname="字段名称" required="1"/>
                            </td>
                            <td>
                            字段类型：
                            </td>
                            <td>
                                <select id="columnType" name="columnType">
                                    <option value="string">string</option>
                                    <option value="integer">integer</option>
                                    <option value="float">float</option>
                                    <option value="date">date</option>
                                    <option value="enum">enum</option>
                                    <option value="enums">enums</option>
                                </select>
                            </td>
                            <td>
                            字段备注：
                            </td>
                            <td>
                                <input type="text" id="columnRemark" name="templateName" elname="字段备注" required="1" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                            是否只读：
                            </td>
                            <td>
                                <select id="columnReadFlag" name="columnReadFlag">
                                    <option value="false">否</option>
                                    <option value="true">是</option>
                                </select>
                            </td>
                            <td>
                            是否可空：
                            </td>
                            <td>
                                <select id="columnNullFlag" name="columnNullFlag">
                                    <option value="true">是</option>
                                    <option value="false">否</option>
                                </select>
                            </td>
                            <td>
                            字段排序：
                            </td>
                            <td>
                                <input type="text" id="columnSeq" name="templateName" elname="字段排序" required="1" pattern="integer"/>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="6">
                                <img src="${ctx}/images/common/button_insert.jpg" title="添加字段" style="cursor:pointer" onclick="insertTemplateColumn()"/>
                                <img src="${ctx}/images/common/button_update.jpg" title="修改字段" style="cursor:pointer" onclick="updateTemplateColumn()"/>
                                <img src="${ctx}/images/common/button_delete.jpg" title="删除字段" style="cursor:pointer" onclick="deleteTemplateColumn()"/>
                            </td>
                        </tr>
                    </table>
                    </form>
                    <SCRIPT LANGUAGE="JavaScript" type="">
                    var templateColumnXThead = new Array(
                    new XThead("选择", "radio"),
                    new XThead("列名"),
                    new XThead("类型"),
                    new XThead("备注"),
                    new XThead("只读"),
                    new XThead("可空"),
                    new XThead("列排序")
                    );

                    var templateColumnXTable = new XTable("templateColumn", templateColumnXThead, true);

                    templateColumnXTable.XTbodyArray = function (model) {
                        var array = new Array(
                        new XTbody("templateColumn", model.columnId, "radio", "onclick='setTemplateColumn(this)'", model),
                        new XTbody("templateColumn", model.columnType == "enum" || model.columnType == "enums" ? "<a href='#' onclick='getTemplateColumnEnum(\"" + model.columnId + "\")'>" + model.columnName + "</a>" : model.columnName),
                        new XTbody("templateColumn", model.columnType == "enum" || model.columnType == "enums" ? "<a href='#' onclick='getTemplateColumnEnum(\"" + model.columnId + "\")'>" + model.columnType + "</a>" : model.columnType),
                        new XTbody("templateColumn", model.columnRemark),
                        new XTbody("templateColumn", model.columnReadFlag),
                        new XTbody("templateColumn", model.columnNullFlag),
                        new XTbody("templateColumn", model.columnSeq)
                        );
                        return array;
                    }

                    templateColumnXTable.getXList = function (page){
                        var templateId = templateXTable.getRadioValue();
                        var templateColumn = new TemplateColumn();
                        templateColumn.templateId = templateId;
                        TemplateColumnService.selectTemplateColumn(templateColumn, function(list){templateColumnXTable.addTable(list);});
                    }

                    function selectTemplateColumn(){
                        templateColumnXTable.goPage(1);
                    }
                    </SCRIPT>
                </div>
                <div class="tab-page" id="tabPage1_3" width="100%">
                    <h2 class="tab">
                    模板授权
                    </h2>
                    <script type="text/javascript">
                    tabPane1.addTabPage(document.getElementById("tabPage1_3"));
                    </script>
                    <table>
                        <tr>
                            <td align="right">
                                <button onclick="insertTemplateAccount()">模板授权</button>
                            </td>
                        </tr>
                    </table>
                    <SCRIPT LANGUAGE="JavaScript" type="">
                    var accountXThead = new Array(
                    new XThead("", "checkbox"),
                    new XThead("用户")
                    );

                    var accountXTable = new XTable("account", accountXThead, true);

                    accountXTable.XTbodyArray = function (model) {
                        var array = new Array(
                        new XTbody("account", model.accountId, "checkbox"),
                        new XTbody("account", model.accountName)
                        );
                        return array;
                    }

                    accountXTable.getXList = function (page){
                        var account = new Account();
                        SecurityService.selectAccountList(account, function(list){accountXTable.addTable(list);});
                    }
                    </SCRIPT>
                </div>
            </div>
        </td>
    </tr>
</table>

<DIV id="templateColumnEnumDiv" name="templateColumnEnumDiv" class="DIV" style="width:50%;height:50%;display:none;position:absolute;top:150px;left:300px;;z-index:13">
    <table width="100%">
        <thead class="THEAD">
            <tr bgcolor="#f0ce85">
                <td>&nbsp;
                    <img alt="" src="${ctx}/images/common/nav.gif"/>
                    枚举值管理
                </td>
                <td align="right">
                    <img alt="关闭" src="${ctx}/images/common/close.gif" onclick="hiddenTemplateColumnEnumDiv()"/>
                    &nbsp;
                </td>
            </tr>
        </thead>
    </table>
    <form id="templateColumnEnumForm" name="templateColumnEnumForm" action="">
    <center>
    枚举值
    <input type="text" id="enumValue" name="enumValue" elname="枚举值" required="1"/>
    描述
    <input type="text" id="enumText" name="enumText" elname="描述" required="1"/>
    排序
    <input type="text" id="enumSeq" name="enumSeq" elname="排序" required="1" pattern="integer"/>
    <br />
    <button onclick="insertTemplateColumnEnum()">添加</button>
    <button onclick="updateTemplateColumnEnum()">修改</button>
    <button onclick="deleteTemplateColumnEnum()">删除</button>
    </center>
    </form>
    <SCRIPT LANGUAGE="JavaScript" type="">
    var templateColumnEnumXThead = new Array(
    new XThead("选择", "radio"),
    new XThead("枚举值"),
    new XThead("描述"),
    new XThead("排序")
    );

    var templateColumnEnumXTable = new XTable("templateColumnEnum", templateColumnEnumXThead, true);

    templateColumnEnumXTable.XTbodyArray = function (model) {
        var array = new Array(
        new XTbody("templateColumnEnum", model.enumId, "radio", "onclick='setTemplateColumnEnum(this)'", model),
        new XTbody("templateColumnEnum", model.enumValue),
        new XTbody("templateColumnEnum", model.enumText),
        new XTbody("templateColumnEnum", model.enumSeq)
        );
        return array;
    }

    templateColumnEnumXTable.getXList = function (page){
        var columnId = templateColumnXTable.getRadioValue();
        var templateColumnEnum = new TemplateColumnEnum();
        templateColumnEnum.columnId = columnId;
        TemplateColumnEnumService.selectTemplateColumnEnum(templateColumnEnum, function(list){templateColumnEnumXTable.addTable(list);});
    }

    function selectTemplateColumnEnum(){
        templateColumnEnumXTable.goPage(1);
    }
    </SCRIPT>
</DIV>

</body>
</html>
<script type="text/javascript">
function init(){
    selectTemplate();
    accountXTable.goPage(1);
}

function setTemplate(obj){
    var template = obj.parentElement.parentElement.model
    dwr.util.setValue("templateName", template.templateName);
    selectTemplateColumn();
    TemplateService.selectTemplateAccount(template.templateId, callbackSelectTemplateAccount);
}

function callbackSelectTemplateAccount(accountIds){
    accountXTable.setCheckBoxValue(accountIds);
}

function insertTemplate(){
    if(validate($("templateForm"))){
        var template = new Template();
        template.templateName = dwr.util.getValue("templateName");
        TemplateService.insertTemplate(template, selectTemplate);
    }
}

function updateTemplate(){
    if(validate($("templateForm"))){
        var template = new Template();
        template.templateId = templateXTable.getRadioValue();
        template.templateName = dwr.util.getValue("templateName");
        if(template.templateId == null){
            alert("请选择要修改的模版");
            return false;
        }
        TemplateService.updateTemplate(template, selectTemplate);
    }
}

function deleteTemplate(){
    if(confirm("确定删除选择数据")){
        var templateId = templateXTable.getRadioValue();
        if(templateId == null){
            alert("请选择要删除的模版");
            return false;
        }
        TemplateService.deleteTemplate(templateId, selectTemplate);
    }
}

function setTemplateColumn(obj){
    var templateColumn = obj.parentElement.parentElement.model
    dwr.util.setValue("columnName", templateColumn.columnName);
    dwr.util.setValue("columnType", templateColumn.columnType);
    dwr.util.setValue("columnRemark", templateColumn.columnRemark);
    dwr.util.setValue("columnReadFlag", templateColumn.columnReadFlag);
    dwr.util.setValue("columnNullFlag", templateColumn.columnNullFlag);
    dwr.util.setValue("columnSeq", templateColumn.columnSeq);
}

function insertTemplateColumn(){
    if(validate($("templateColumnForm"))){
        var templateColumn = new TemplateColumn();
        templateColumn.columnName = dwr.util.getValue("columnName");
        templateColumn.columnType = dwr.util.getValue("columnType");
        templateColumn.columnRemark = dwr.util.getValue("columnRemark");
        templateColumn.columnReadFlag = dwr.util.getValue("columnReadFlag");
        templateColumn.columnNullFlag = dwr.util.getValue("columnNullFlag");
        templateColumn.columnSeq = dwr.util.getValue("columnSeq");
        templateColumn.templateId = templateXTable.getRadioValue();
        if(templateColumn.templateId == null){
            alert("请选择要定制的模版");
            return false;
        }
        TemplateColumnService.insertTemplateColumn(templateColumn, selectTemplateColumn);
    }
}

function updateTemplateColumn(){
    if(validate($("templateColumnForm"))){
        var templateColumn = new TemplateColumn();
        templateColumn.columnName = dwr.util.getValue("columnName");
        templateColumn.columnType = dwr.util.getValue("columnType");
        templateColumn.columnRemark = dwr.util.getValue("columnRemark");
        templateColumn.columnReadFlag = dwr.util.getValue("columnReadFlag");
        templateColumn.columnNullFlag = dwr.util.getValue("columnNullFlag");
        templateColumn.columnSeq = dwr.util.getValue("columnSeq");
        templateColumn.columnId = templateColumnXTable.getRadioValue();
        if(templateColumn.columnId == null){
            alert("请选择要修改的字段");
            return false;
        }
        TemplateColumnService.updateTemplateColumn(templateColumn, selectTemplateColumn);
    }
}

function deleteTemplateColumn(){
    if(confirm("确定删除选择数据")){
        var templateColumnId = templateColumnXTable.getRadioValue();
        if(templateColumnId == null){
            alert("请选择要删除的字段");
            return false;
        }
        var templateColumn = new TemplateColumn();
        templateColumn.columnId = templateColumnId;
        TemplateColumnService.deleteTemplateColumn(templateColumn, selectTemplateColumn);
    }
}

function insertTemplateAccount(){
    var templateId = templateXTable.getRadioValue();
    var accountIds = accountXTable.getCheckBoxValue();

    if(templateId == null){
        alert("请选择要授权的模版");
        return false;
    }

    if(accountIds.length == 0){
        alert("请选择要授权的用户");
        return false;
    }
    TemplateService.insertTemplateAccount(templateId, accountIds);
}

function getTemplateColumnEnum(columnId){
    templateColumnXTable.setRadioValue(columnId);
    selectTemplateColumnEnum();
    showTemplateColumnEnumDiv();
}

function showTemplateColumnEnumDiv(){
    $("templateColumnEnumDiv").style.display = "block";
}

function hiddenTemplateColumnEnumDiv(){
    $("templateColumnEnumDiv").style.display = "none";
}

function setTemplateColumnEnum(obj){
    var templateColumnEnum = obj.parentElement.parentElement.model
    dwr.util.setValue("enumValue", templateColumnEnum.enumValue);
    dwr.util.setValue("enumText", templateColumnEnum.enumText);
    dwr.util.setValue("enumSeq", templateColumnEnum.enumSeq);
}

function insertTemplateColumnEnum(){
    if(validate($("templateColumnEnumForm"))){
        var templateColumnEnum = new TemplateColumnEnum();
        templateColumnEnum.enumValue = dwr.util.getValue("enumValue");
        templateColumnEnum.enumText = dwr.util.getValue("enumText");
        templateColumnEnum.enumSeq = dwr.util.getValue("enumSeq");
        templateColumnEnum.columnId = templateColumnXTable.getRadioValue();
        if(templateColumnEnum.columnId == null){
            alert("请选择要绑定的字段");
            return false;
        }
        TemplateColumnEnumService.insertTemplateColumnEnum(templateColumnEnum, selectTemplateColumnEnum);
    }
}

function updateTemplateColumnEnum(){
    if(validate($("templateColumnEnumForm"))){
        var templateColumnEnum = new TemplateColumnEnum();
        templateColumnEnum.enumValue = dwr.util.getValue("enumValue");
        templateColumnEnum.enumText = dwr.util.getValue("enumText");
        templateColumnEnum.enumSeq = dwr.util.getValue("enumSeq");
        templateColumnEnum.enumId = templateColumnEnumXTable.getRadioValue();
        if(templateColumnEnum.enumId == null){
            alert("请选择要修改的枚举值");
            return false;
        }
        TemplateColumnEnumService.updateTemplateColumnEnum(templateColumnEnum, selectTemplateColumnEnum);
    }
}

function deleteTemplateColumnEnum(){
    if(confirm("确定删除选择数据")){
        var templateColumnEnumId = templateColumnEnumXTable.getRadioValue();
        if(templateColumnEnumId == null){
            alert("请选择要删除的数据");
            return false;
        }
        var templateColumnEnum = new TemplateColumnEnum();
        templateColumnEnum.enumId = templateColumnEnumId;
        TemplateColumnEnumService.deleteTemplateColumnEnum(templateColumnEnum, selectTemplateColumnEnum);
    }
}

</script>
